<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>菜单管理</title>
    <link rel="stylesheet" href="__PUBLIC__/easyweb/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="__PUBLIC__/easyweb/assets/module/admin.css?v=318"/>
    <style>
        @media screen and (min-width: 992px){
            #authoritiesEditForm .layui-col-md6{width: unset;float: none}
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="authoritiesTable"></table>
        </div>
    </div>
</div>
<script type="text/html" id="authoritiesTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="authoritiesEditDialog">

    <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
          style="padding-right: 20px;">
        <input name="id" type="hidden"/>
        <div class="layui-row">
            <div class="layui-col-md6">

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">Sku名称:</label>
                    <div class="layui-input-block">
                        <input name="spec_name" placeholder="请输入名称" class="layui-input"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单:</label>
                    <div class="layui-input-block">
                        <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">价格:</label>
                    <div class="layui-input-block">
                        <input name="price" placeholder="请输入排序号"  class="layui-input"
                               lay-verType="tips"  />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">库存:</label>
                    <div class="layui-input-block">
                        <input name="stock" placeholder="请输入排序号"  class="layui-input"
                               lay-verType="tips"  />
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
    
</script>
<script type="text/javascript" src="__PUBLIC__/easyweb/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="__PUBLIC__/easyweb/assets/js/common.js?v=318"></script>
<script type="text/html" id="raQTmc">
    <div class="layui-form-item">
        <label class="layui-form-label">url:</label>
        <div class="layui-input-block">
            <input name="url" placeholder="请输入url" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图标:</label>
        <div class="layui-input-block">
            <input type ="hidden" name="icon" placeholder="请输入图标" class="layui-input" id="icon_menu"/>
            <input type="text" id="iconPicker3-2" lay-filter="iconPicker3-2" class="layui-input">
        </div>
    </div>
</script>
<script type="text/html" id="raQTf">
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">权限标识</label>
        <div class="layui-input-block">
            <input type="radio" name="perms" lay-verify="perms" value="1" title="查看">
            <input type="radio" name="perms" lay-verify="perms" value="2" title="添加">
            <input type="radio" name="perms" lay-verify="perms" value="3" title="修改">
            <input type="radio" name="perms" lay-verify="perms" value="4" title="删除">
        </div>
    </div>
</script>
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect','iconPicker'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var xmSelect = layui.xmSelect;
        var tbDataList = [];
        var iconPicker = layui.iconPicker;
        
        var admin_data;

        function iconXuanze(){
            iconPicker.render({
                elem: '#iconPicker3-2',
                type: 'fontClass',
                search: true,
                page: true,
                limit: 12,
                click: function (data) {
                    $('#icon_menu').val(data.icon);
                },
                success: function(d) {
                }
            });
            iconPicker.checkIcon('iconPicker3-2', 'layui-icon-cellphone');
            $('#icon_menu').val('layui-icon-cellphone');
        }
        var insTb = treeTable.render({
            elem: '#authoritiesTable',
            url: '{:U("ShoppingMall/sku_ajax")}',
            where: {type:'menu'},
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            tree: {
                iconIndex: 2,
                idName: 'id',
                pidName: 'p_id',
                isPidData: true
            },
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'spec_name', title: 'Sku名称', minWidth: 180},
                {field: 'price', title: '价格', align: 'center', width: 120},
                {field: 'stock', title: '库存', align: 'center', width: 120},
                {field: 'add_time', title: '创建时间', align: 'center', width: 120},
                {title: '操作', toolbar: '#authoritiesTbBar', align: 'center', width: 120}
            ]],
            parseData: function (res) {
                //res 即为原始返回的数据
                console.log(res);
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    //"count": res.data.items.length, //解析数据长度
                    "data": res.data, //解析数据列表
                    "admin":res.admin
                };
            },
            
        });
        treeTable.on('tool(authoritiesTable)', function (obj) {
            if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    ,title: '编辑sku'
                    ,content: "{:U('shoppingMall/save_sku')}"+"?id="+obj.data.id
                    ,maxmin: true
                    ,area: ['60%', '60%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            
                            var m_show = 0;
                            if (field.m_show != undefined){
                                m_show = field.m_show;
                            }
                            console.log(field)
  
        
                            $.ajax({
                                type:"post",
                                url:"{:U('shoppingMall/save_ajax_sku')}",
                                data: {
                                    id:field.id,
                                    spec_name:field.spec_name,
                                    goods_id:field.goods_id,
                                    price:field.price,
                                
                                    p_id:field.p_id,
                                    stock:field.stock,
                                    
                                },
                                dataType:"json",
                                beforeSend: function () {
                                    layer.load(1);
                                },
                                success:function(res){
                                    layer.close(layer.index);
                                    if(res.code == 1001){
                                        layer.closeAll();
                                        table.reload('LAY-table',{where: {time:new Date()}}); //数据刷新
                                    }
                                    else {
                                        layer.msg(res.msg);
                                    }
                                }
                            });

                        });

                        submit.trigger('click');
                    }
              
                });
                ///////
                /*showEditModel(obj.data);
                $("#menu_type_box").hide();*/
            } else if (obj.event === 'del') {
                doDel(obj);
            }
        });
        treeTable.on('toolbar(authoritiesTable)', function (obj) {
            if (obj.event === 'add') {
                showEditModel();
            } else if (obj.event === 'del') {
                var checkRows = insTb.checkStatus();
                // console.log(checkRows);
                if (checkRows.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.map(function (d) {
                    // console.log(d.id);
                    return d.id;
                });
                var level = null;
                if( checkRows.length == 2){
                    level = checkRows[1].level;
                }else{
                    level = checkRows[0].level;
                }
                doDel({ids: ids,level: level});
            }
        });
        function changeQT(value,type) {
            if (value == "F") {
                $('#qaRIGroup').html($('#raQTf').html());
                form.render('radio');
                form.render('select', 'authoritiesEditForm');
                form.verify({
                    perms: function(value){
                        if( typeof($('input[name="perms"]:checked').val()) == "undefined"){
                            return '权限标识必选';
                        }
                    }
                });
            } else {
                $('#qaRIGroup').html($('#raQTmc').html());
                iconXuanze();
                form.render('select', 'authoritiesEditForm');
            }
        }
        function showEditModel(mData) {

            admin.open({
                type: 1,
                area: '600px',
                title: (mData ? '修改' : '添加') + 'Sku',
                content: $('#authoritiesEditDialog').html(),
                success: function (layero, dIndex) {
                    changeQT('M');
                    form.on('radio(raQT)', function (data) {
                        changeQT(data.value);
                    });
                    if (mData && mData.menu_type) {
                        if(mData.menu_type == "F"){
                            changeQT('F');
                        }else{
                            if(mData.icon != "" || mData.icon == null ){
                                iconPicker.checkIcon('iconPicker3-2', mData.icon);
                            }else{
                                iconPicker.checkIcon('iconPicker3-2', '');
                            }
                        }
                    }
                    form.val('authoritiesEditForm', mData);
                    form.on('submit(authoritiesEditSubmit)', function (data) {
                        data.field.p_id = insXmSel.getValue('valueStr');
                        if( insXmSel.getValue().length == 0 ){
                            data.field.f_level = 0;
                        }else{
                            data.field.f_level = insXmSel.getValue()[0].level;
                        }
                        var loadIndex = layer.load(2);
                        $.post(mData ? '{:U("ShoppingMall/p_Edit")}' : '{:U("ShoppingMall/p_Add")}', {data: data.field,type:'menu'}, function (res) {
                            
                            layer.close(loadIndex);
                            if (res.code === 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.refresh();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                    var insXmSel = xmSelect.render({
                        el: '#authoritiesEditParentSel',
                        height: '250px',
                        data: insTb.options.data,
                        initValue: mData ? [mData.p_id] : [],
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'spec_name',
                            value: 'id'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: []
                        }
                    });
                  
                   
                    /*var insXmSel = xmSelect.render({
                        el: '#authoritiesEditShops',
                        height: '250px',
                        data: admin_data,
                        initValue: mData ? [mData.shop_id] : [],
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'admin_name',
                            value: 'admin_id'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: []
                        }
                    });*/
                    
                    
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }
        function doDel(obj) {
            // console.log(obj);
            if(obj.hasOwnProperty('data')){
                if(obj.data.hasOwnProperty('children')){
                    layer.msg("当前分类存在未删除下级，请先删除下级或使用选框批量删除", {icon: 2});
                    error;
                }
            }
            var f_level = null;
            if(obj.data){
                f_level = obj.data.level;
            }else{
                f_level = obj.level;
            }
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('{:U("ShoppingMall/p_Delete")}', {id: obj.data ? obj.data.id : obj.ids}, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.refresh();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

    });
</script>
</body>
</html>